<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\Orderinfo;

class InstockController extends Controller
{
	public function actionIndex()
	{	
		$query = new \yii\db\Query();

		$displayAll = Yii::$app->request->get("displayAll",0);

		if($displayAll == 1){
			$list = $query->select("*")->from("JXC_INSTOCK")->orderBy("INSTOCKID DESC")->all();
		}else{
			$list = $query->select("*")->from("JXC_INSTOCK")->orderBy("INSTOCKID DESC")->limit(DEFAULT_LIMIT)->all();
		}


		$sumCount = 0;
		foreach ($list as  $value) {
			$sumCount += $value['COUNT'];
		}
		
		return $this->render("index",['list' => $list,'sumCount'=>$sumCount]);
	}

	private function generateQueryString($fieldName){
		if (empty($fieldName))
			return "1 = 1";
		$getFieldNameRequest = $_GET[$fieldName];
		if (empty($getFieldNameRequest))
			return "1 = 1";
		else
			return $fieldName." like '%".$getFieldNameRequest."%'";
	}

	public function actionSearch()
	{
		$intime = $_GET['intime'];
		$intime_end = $_GET['intime_end'];
		$productiondate = $_GET['productiondate'];
		$productiondate_end = $_GET['productiondate_end'];

		$plannum = $_GET['plannum'];
		$productname = $_GET['productname'];
		$drawingno   = $_GET['drawingno'];
		$instocknum   = $_GET['instocknum'];

		$query = new \yii\db\Query();
		$query->select("*")->from("JXC_INSTOCK");

		$instockidList = "";
		if ($productname != "" || $drawingno != null){
			$instockidList = $this->getStockidList($productname, $drawingno);
		}

		if (!empty($instockidList)){
			$query->andWhere("INSTOCKID in (".$instockidList.")");
		}

		if (!empty($instocknum)){
			$query->andWhere("instocknum like '%".$instocknum."%'");
		}

		if (!empty($plannum)){
			$query->andWhere("plannum like '%".$plannum."%'");
		}

		if(!empty($intime))
		{
			$query->andWhere("intime >= '$intime'");
		}

		if(!empty($intime_end))
		{
			$query->andWhere("intime <= '".$intime_end." 23:59:59'");
		}

		if(!empty($productiondate))
		{
			$query->andWhere("productiondate >= '$productiondate'");
		}

		if(!empty($productiondate_end))
		{
			$query->andWhere("productiondate <= '".$productiondate_end." 23:59:59'");
		}

		/*
		if(!empty($ordernum))
		{
			$productidList = Orderinfo::getProductidByOrdernum($ordernum);

			if (empty($productidList)){
				$query->andWhere('productid = 0');
			}else{
				$query->andWhere("productid in (".$productidList.")");
			}
		}
		*/

		$list = $query->all();

		$sumCount = 0;
		foreach ($list as  $value) {
			$sumCount += $value['COUNT'];
		}

		return $this->render("index",['list' => $list,'sumCount'=>$sumCount]);
	}

	public function actionItem()
	{
		$instockid = $_GET['id'];
		$query = new \yii\db\Query();
		$list = $query->select("*")->from("JXC_INSTOCKITEM")->where('instockid = '.$instockid)->all();

		$sumCount = 0;
		foreach ($list as  $value) {
			$sumCount += $value['COUNT'];
		}

		$result = array();

		foreach($list as $value){
			$tempresult = array();
			$tempresult = $value;
			$planbillItemid = $value['PLANBILLITEMID'];
			$productid      = $value['PRODUCTID'];
			$planBillInfo = $this->getPlanBillInfo($planbillItemid,$productid);
			$tempresult['INSTOCKSTATUS'] = $planBillInfo['INSTOCKSTATUS'];
			$tempresult['INCOUNT']        = $planBillInfo['INCOUNT'];
			$tempresult['WILLINCOUNT']   = $planBillInfo['WILLINCOUNT'];
			$tempresult['POSITION']       = $planBillInfo['POSITION'];
			$result[] = $tempresult;
		}

		return $this->render("item",['list' => $result,'sumCount'=>$sumCount]);
	}

	public function getPlanBillInfo($planbillItemid,$productid){
		$result = array();
		$result['INSTOCKSTATUS'] = "";
		$result['INCOUNT'] = "";
		$result['WILLINCOUNT'] = "";
		$result['POSITION'] = "";

		if (empty($planbillItemid)){
			return $result;
		}

		$query = new \yii\db\Query();
		$planbillItem = $query->select("*")
						->from("JXC_PLANBILLITEM")
						->where("PLANBILLITEMID = ".$planbillItemid)
						->one();

		if (empty($planbillItem)){
			return $result;
		}

		$result['INCOUNT'] = $planbillItem['INCOUNT'];
		$result['WILLINCOUNT'] = $planbillItem['WILLINCOUNT'];

		$planbillid = $planbillItem['PLANBILLID'];

		if (empty($planbillid))
			return $result;

		$query = new \yii\db\Query();
		$planbill = $query->select("*")
						->from("JXC_PLANBILL")
						->where("PLANBILLID = ".$planbillid)
						->one();

		if (empty($planbill)){
			return $result;
		}

		$result["INSTOCKSTATUS"] = $planbill['INSTOCKSTATUS'];

		$planbillnum = $planbill['PLANBILLNUM'];

		if ((!empty($planbillnum)) && (!empty($productid))){
			$query = new \yii\db\Query();
			$inventoryInfo = $query->select("POSITION")
				->from("JXC_INVENTORY")
				->andWhere("PRODUCTID = ".$productid)
				->andWhere("PLANNUM = '".$planbillnum."'")
				->one();

			if (!empty($inventoryInfo))
				$result['POSITION'] = $inventoryInfo['POSITION'];
		}

		return $result;
	}

	public function getStockidList($productname, $drawingno){
		$query = new \yii\db\Query();
		$query->select("INSTOCKID")->from("JXC_INSTOCKITEM");

		if (!empty($productname)){
			$query->andWhere("PRODUCTNAME like '%".$productname."%'");
		}

		if (!empty($drawingno)){
			$query->andWhere("DRAWINGNO like '%".$drawingno."%'");
		}

		$stockidList = $query->all();
		$result = array();

		foreach($stockidList as $stockid){
			$result[] = $stockid['INSTOCKID'];
		}

		return implode(",",$result);

	}
}